/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bishe.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.IdGen;
import com.thinkgem.jeesite.modules.bishe.entity.*;
import com.thinkgem.jeesite.modules.bishe.service.*;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;

/**
 * 订单管理Controller
 * @author yue
 * @version 2018-12-26
 */
@Controller
@RequestMapping(value = "${adminPath}/bishe/bisheOrder")
public class BisheOrderController extends BaseController {

	@Autowired
	private BisheOrderService bisheOrderService;
	@Autowired
	private BisheClientService bisheClientService;
	@Autowired
	private BisheGoodsService bisheGoodsService;
	@Autowired
	private BishePackService bishePackService;
	@Autowired
	private BisheGoodsLogService bisheGoodsLogService;
	
	@ModelAttribute
	public BisheOrder get(@RequestParam(required=false) String id) {
		BisheOrder entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bisheOrderService.get(id);
		}
		if (entity == null){
			entity = new BisheOrder();
		}
		return entity;
	}
	
	@RequiresPermissions("bishe:bisheOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(BisheOrder bisheOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BisheOrder> page = bisheOrderService.findPage(new Page<BisheOrder>(request, response), bisheOrder);
		model.addAttribute("page", page);
		return "modules/bishe/bisheOrderList";
	}

	@RequiresPermissions("bishe:bisheOrder:view")
	@RequestMapping(value = "form")
	public String form(BisheOrder bisheOrder, Model model) {
		model.addAttribute("bisheOrder", bisheOrder);
		return "modules/bishe/bisheOrderForm";
	}

	@RequestMapping(value = "relation")
	public String relation(Model model) {
		model.addAttribute("goods_type",DictUtils.getDictList("goods_type"));
		return "modules/bishe/relation";
	}

	@RequiresPermissions("bishe:bisheOrder:edit")
	@RequestMapping(value = "save")
	public String save(BisheOrder bisheOrder, BishePack pack,BisheGoodsLog log,Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, bisheOrder)){
			return form(bisheOrder, model);
		}
		System.err.println(bisheOrder.getOrderType());
		Enumeration<String> paraNames=request.getParameterNames();
		String str="";
		Integer prace=0;
		String id= IdGen.uuid();
		bisheOrder.setId(id);
		for(Enumeration<String> e = paraNames; e.hasMoreElements();){
			String thisName=e.nextElement().toString();
			String thisValue=request.getParameter(thisName);
			if(thisName.startsWith("table")){
				thisName.substring(5);
				BisheGoods good=bisheGoodsService.get(thisName.substring(5));
				if(good!=null && Integer.parseInt(thisValue)>0){

					if("2".equals(bisheOrder.getOrderType()) || "4".equals(bisheOrder.getOrderType()) || "6".equals(bisheOrder.getOrderType())){
						good.setSurplusNum(good.getSurplusNum()-Integer.parseInt(thisValue));
					}else {
						good.setSurplusNum(good.getSurplusNum()+Integer.parseInt(thisValue));
					}

					bisheGoodsService.save(good);
					// 物品日志
					log.setOrder(bisheOrder);
					log.setGoods(good);
					log.setUseNum(Integer.parseInt(thisValue)+"");
					bisheGoodsLogService.save(log);
					log.setId(null);
					str+=good.getName()+": 数量"+thisValue+",";
					try {
						prace+=(good.getGoodsPrice()*Integer.parseInt(thisValue));
					}catch (Exception ee){

					}
				}

			}

		}
		bisheOrder.setOrderContent(str);
		bisheOrderService.save2(bisheOrder);
		// 生成合同
		pack.setOrderType(bisheOrder.getOrderType());
		bisheOrder.setId(id);
		pack.setOrder(bisheOrder);
		pack.setPackTitle(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+DictUtils.getDictLabel(bisheOrder.getOrderType(),"order_type",""));
		pack.setPackContent(str);
		pack.setPackDate(new Date());
		if("2".equals(bisheOrder.getOrderType()) || "4".equals(bisheOrder.getOrderType()) || "6".equals(bisheOrder.getOrderType())){
			pack.setRentMoney(prace+"");
		}else {
			pack.setRentMoney("-"+prace);
		}
		pack.setJiesuanFlag(0);
		bishePackService.save(pack);
		addMessage(redirectAttributes, "保存订单信息成功");
		return "redirect:"+Global.getAdminPath()+"/bishe/bisheOrder/?repage";
	}
	
	@RequiresPermissions("bishe:bisheOrder:edit")
	@RequestMapping(value = "delete")
	public String delete(BisheOrder bisheOrder, RedirectAttributes redirectAttributes) {
		bisheOrderService.delete(bisheOrder);
		addMessage(redirectAttributes, "删除订单信息成功");
		return "redirect:"+Global.getAdminPath()+"/bishe/bisheOrder/?repage";
	}

}